package com.yf1706.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yf1706.bean.ExamArrangement;
import com.yf1706.bean.ExamPaper;
import com.yf1706.bean.ExamPaperQuestion;
import com.yf1706.bean.GapFilling;
import com.yf1706.bean.Judge;
import com.yf1706.bean.Multiselect;
import com.yf1706.bean.SingleChoice;
import com.yf1706.service.ExamArrangementService;
import com.yf1706.service.ExamPaperQuestionService;
import com.yf1706.service.ExamPaperService;

@Controller
@RequestMapping("exam2")
public class ExamController2{
	
	@Autowired
	private ExamArrangementService eas;
	
	@Autowired
	private ExamPaperService eps;
	
	@Autowired
	private ExamPaperQuestionService epqs;
	
	
	
	
	@RequestMapping("getAllExamArrangement")
	@ResponseBody
	public Map<String,Object> getAllExamArrangement(int page,int rows){
		Page<ExamArrangement> pages = PageHelper.startPage(page, rows);
		List<ExamArrangement> list = eas.queryAllExamArrangement();
		long total = pages.getTotal();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
	
	//
	@RequestMapping("ExamArrangeAdd")
	@ResponseBody
	public void ExamArrangeAdd(HttpServletRequest request) throws ParseException {
		String examAN=request.getParameter("examAN");
		String examP=request.getParameter("examP");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println(request.getParameter("eaStartTime"));
		Date eaStartTime = sdf.parse(request.getParameter("eaStartTime"));
		Date eaEndTime = sdf.parse(request.getParameter("eaEndTime"));
		int allScore=Integer.parseInt(request.getParameter("AllScore"));
		int snum=Integer.parseInt(request.getParameter("snum"));
		int ss=Integer.parseInt(request.getParameter("ss"));
		int mnum=Integer.parseInt(request.getParameter("mnum"));
		int ms=Integer.parseInt(request.getParameter("ms"));
		int jnum=Integer.parseInt(request.getParameter("jnum"));
		int js=Integer.parseInt(request.getParameter("js"));
		int fnum=Integer.parseInt(request.getParameter("fnum"));
		int fs=Integer.parseInt(request.getParameter("fs"));
		eas.add(examAN,examP,eaStartTime,eaEndTime,"0",allScore,snum,ss,mnum,ms,jnum,js,fnum,fs);
	}
	
	@RequestMapping("ExamArrangeUpdata")
	@ResponseBody
	public void ExamArrangeUpdata(HttpServletRequest request) throws ParseException {
		int examArrangementId=Integer.parseInt(request.getParameter("examArrangementId"));
		int examPaperId=Integer.parseInt(request.getParameter("examPaperId"));
		String examAN=request.getParameter("examAN");
		String examP=request.getParameter("examP");
		eps.updataNameById(examPaperId,examP);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date eaStartTime = sdf.parse(request.getParameter("eaStartTime"));
		Date eaEndTime = sdf.parse(request.getParameter("eaEndTime"));
		eas.updataExamArrange(examArrangementId,examAN,examPaperId,eaStartTime,eaEndTime,Integer.parseInt("0"));
	}
	
	//删除
	@RequestMapping("deleteExamArrangement")
	@ResponseBody
	public  void deleteExamArrangement(HttpServletRequest request){
		String value=request.getParameter("value");
		String[] val=value.split(",");
		String value1=request.getParameter("value1");
		System.err.println(value+" "+value1);
		String[] val1=value1.split(",");
		System.err.println(val.length);
		for(int i=0;i<val.length;i++) {
			int ExamArrangementId=Integer.parseInt(val[i]);
			int ExamPaperId=Integer.parseInt(val1[i]);
			eas.deleteByExamArrangementId(ExamArrangementId);
			eps.deleteByExamPaperId(ExamPaperId);
			
		}
		
		
	}
	@RequestMapping("ExamArrangementSearch")
	@ResponseBody
	public Map<String,Object> ExamArrangementSearch(int page,int rows,HttpServletRequest request){
		Page<ExamArrangement> pages = PageHelper.startPage(page, rows);
		String examArrangementName=request.getParameter("ExamArrangementName");
		List<ExamArrangement> list=eas.queryByExamArrangementName(examArrangementName);
		long total = pages.getTotal();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
	
	//
	@RequestMapping("getAllExamScore")
	@ResponseBody
	public Map<String,Object> getAllExamScore(int page,int rows){
		Page<ExamPaper> pages = PageHelper.startPage(page, rows);
		List<ExamPaper> list = eps.queryAllExamPaper1();
		long total = pages.getTotal();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	
	}
	
	@RequestMapping("addTM")
	@ResponseBody
	public void addTM(HttpServletRequest request) {
		int qbId=Integer.parseInt(request.getParameter("qbId"));//题库
		int examPaperId=Integer.parseInt(request.getParameter("examPaperId"));
		String examPaperSnum=request.getParameter("examPaperSnum");
		String examPaperMnum=request.getParameter("examPaperMnum");//数量
		String examPaperJnum=request.getParameter("examPaperJnum");
		String examPaperFnum=request.getParameter("examPaperFnum ");
		eps.addTM(qbId,examPaperId,examPaperSnum,examPaperMnum,examPaperJnum,examPaperFnum);
	}
	
	//随机生成题目，并将其存储到各自类型的list集合中用于在页面上渲染出来
	@RequestMapping("shijuanInfo")
	@ResponseBody
	public ModelAndView shijuanInfo(Object row, HttpSession session,HttpServletRequest request) {
		ExamPaper ep=(ExamPaper)row;
		
		session.setAttribute("s", "");
		session.setAttribute("s", "");
		session.setAttribute("s", "");
		session.setAttribute("s", "");
		/*String examPaperScore=request.getParameter("examPaperScore");
		String sfen=request.getParameter("examPaperS");
		String ffen=request.getParameter("examPaperF");
		String mfen=request.getParameter("examPaperM");
		String jfen=request.getParameter("examPaperJ");*/
		session.setAttribute("examPaperScore", ep.getExamPaperScore());
		session.setAttribute("sfen", ep.getExamPaperS());
		session.setAttribute("mfen", ep.getExamPaperM());
		session.setAttribute("jfen", ep.getExamPaperJ());
		session.setAttribute("ffen", ep.getExamPaperF());
		List<ExamPaperQuestion> list =epqs.queryByExamPaperId(ep.getExamPaperId(),"单选");
		List<ExamPaperQuestion> list2 =epqs.queryByExamPaperId(ep.getExamPaperId(),"多选");
		List<ExamPaperQuestion> list3 =epqs.queryByExamPaperId(ep.getExamPaperId(),"判断");
		List<ExamPaperQuestion> list4 =epqs.queryByExamPaperId(ep.getExamPaperId(),"填空");
		if(list.size()>0) {
			List<SingleChoice> s=new ArrayList<SingleChoice>();
			for(int i =0;i<list.size();i++) {
				s.add(list.get(i).getSingleChoice());
			}
			session.setAttribute("s", s);//试卷单选题
			session.setAttribute("sl", s.size());//数量
			
		}
		if(list2.size()>0) {
			List<Multiselect> m=new ArrayList<Multiselect>();
			for(int i=0;i<list2.size();i++) {
				m.add(list2.get(i).getMultiselect());
			}
			session.setAttribute("m", m);
			session.setAttribute("ml", m.size());
		}
		if(list3.size()>0) {
			List<GapFilling> f=new ArrayList<GapFilling>();
			for(int i=0;i<list3.size();i++) {
				f.add(list3.get(i).getGapFilling());
			}
			session.setAttribute("f", f);
			session.setAttribute("fl", f.size());
		}
		if(list4.size()>0) {
			List<Judge> j=new ArrayList<Judge>();
			for(int i=0;i<list4.size();i++) {
				j.add(list4.get(i).getJudge());
			}
			session.setAttribute("j", j);
			session.setAttribute("jl", j.size());
		}
		
		
		ModelAndView mv=new ModelAndView("wjl/shijuanInfo");
		
		return mv;
	}
}
